package com.ut.richpath.util;

import ohos.app.Context;

public class Utils {

    public static float vpToPixel(Context context, Float vp){
        return vp * context.getResourceManager().getDeviceCapability().screenDensity/160;
    }

    public static float getDimensionFromString(String value){
        return Float.parseFloat(value.replace("vp", ""));
    }

    public static int getColorFromString(String value) {
        if (value.length() == 7) {
            value = value.replace("#", "#ff");
        } else if (value.length() == 4) {
            value = String.format("#ff%1$c%1$c%2$c%2$c%3$c%3$c", value.charAt(1), value.charAt(2), value.charAt(3));
        } else if (value.length() == 2) {
            value = String.format("#ff%1$c%1$c%1$c%1$c%1$c%1$c", value.charAt(1));
        }
        return value.length() == 9 ? parseColor(value) : 0;
    }

    private static int parseColor(String colorString){
        int alpha = Integer.parseInt(String.format("%c%c", colorString.charAt(1), colorString.charAt(2)), 16);
        int rgb = Integer.parseInt(colorString.substring(3).toLowerCase(), 16);
        return alpha<<24|rgb;
    }

}
